<?
class ss_cli_command_interface extends ss_cli_command{
	private $interface=array();
	
	public function execute(){
		return call_user_func_array(array($this,'generate'),func_get_args());
	}
	
	public function save(){
		file_put_contents(SITE_ROOT_PATH.DIRECTORY_SEPARATOR.'htdocs'.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR.'interface.data',serialize($this->interface));
		$_json=json_encode($this->interface);
		file_put_contents(
			SITE_ROOT_PATH.DIRECTORY_SEPARATOR.'htdocs'.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR.'interface.json',
			$_json);
	}
	
	public function generate(){
		$_schema=$this->dispatch->merge->get('schema');
		$this->dispatch->output("\n*** Generating interfaces ***");
		foreach($_schema as $class=>$data){
			if(isset($data['slipstream']['expose'])){
				$this->dispatch->output("Creating interface for class {$class}");
				$store=array('name'=>$class);
					//print_r($data['actAs']);
				$store['settings']['I18n']=isset($data['actAs']['I18n']);
				$store['settings']['engine']=($store['settings']['I18n']?'slipstream.data.JsonRestStoreTrans':'dojox.data.JsonRestStore');
				$store['arguments']=array(
					'target'=>'/'.$this->settings['server']['api']['api'].'/'.$class.'/',
					'idAttribute'=>'id',
				);
				$label=isset($data['slipstream']['label'])?$data['slipstream']['label']:'name';
				if(isset($data['actAs']['I18n']['fields']) && in_array($label,$data['actAs']['I18n']['fields'])) {
					print("i18n Label {$label}\n");
					$store['arguments']['labelAttribute']='#Translation.{lang}.'.$label;
				} elseif(array_key_exists($label,$data['columns'])){
					print("Normal label: {$label}\n");
					$store['arguments']['labelAttribute']=$label;
				} else {
					print("Accessor label: {$label}\n");
					$store['arguments']['labelAttribute']=$label;
				}
				$schema=array('properties'=>array());
				$relations=array();
				if(isset($data['relations'])){
					foreach($data['relations'] as $relname=>$reldata) $relations[]=(strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $relname)).'_id');
				}
				foreach($data['columns'] as $colname=>$coldata){
					if(in_array($colname,$relations)) {
						print('Skipping foregin key: '.$colname."\n");
						continue;
					}
					if(is_array($coldata)){
						$schema['properties'][$colname]=$this->getTypeData($coldata['type']);
						if(isset($coldata['default'])) $schema['properties'][$colname]['default']=$coldata['default'];
					} else {
						$schema['properties'][$colname]=$this->getTypeData($coldata);
					}
				}
				if(isset($data['actAs']['Timestampable'])){
					$schema['properties']['created_at']=array('type'=>'string','format'=>'date-time');
					$schema['properties']['updated_at']=array('type'=>'string','format'=>'date-time');
				}
				$store['arguments']['schema']=$schema;
				$this->interface[$class]=$store;
			}
		}
		$this->save();
	}
	
	protected function getTypeData($type){
		if(strpos($type,'(')){
			list($type, $size) = explode('(',$type);
			$type=trim($type,' ()');
			$size=trim($size,' ()');
		}
		switch($type){
			case 'string':
			case 'clob':
			case 'blob':
			case 'text':
			case 'varchar':
				return array('type'=>'string');
			case 'int':
			case 'integer':
				return array('type'=>'integer');
			case 'float':
				return array('type'=>'number');
			case 'timestamp':
				return array('type'=>'string','format'=>'date-time');
			default:
				return array('type'=>'any');
		}
	}
}

?>